package LawsonTransformJava.pgm;


//OracleTAEmpServDAO implementation of the 
//TAEmpServDAO interface. This class can contain all
//Oracle specific code and SQL statements. 
//The client is thus shielded from knowing 
//these implementation details.

import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import LawsonTransformJava.xref.XrefDepartment;
import LawsonTransformJava.xref.XrefEmpStatus;
import LawsonTransformJava.xref.XrefJobCode;
import LawsonTransformJava.xref.XrefLocatCode;
import LawsonTransformJava.xref.XrefPosition;
import LawsonTransformJava.xref.XrefProcessLevel;
import LawsonTransformJava.xref.XrefUnionCode;

import LawsonTransformJava.dao.DAOFactoryAccessDB;
import LawsonTransformJava.dao.DAOFactoryOracleDB;
import LawsonTransformJava.dao.DAOTAEmpServ;
import LawsonTransformJava.dao.DAObject;
import LawsonTransformJava.data.TblTAEmpServ;

public class LP553554 extends DAObject implements 
 DAOTAEmpServ, Runnable {

	private XrefJobCode xJobCodes = null;
	private XrefPosition xPositions = null;
	private XrefDepartment xDepartments = null;
	private XrefProcessLevel xProcessLevels = null;
	private XrefEmpStatus xEmpStatuses = null;
	private XrefLocatCode xLocatCodes = null;
	private XrefUnionCode xUnionCodes = null;
	
	private TblTAEmpServ tae = new TblTAEmpServ();

//	private int lp_status = 0;
	
	public LP553554(File output) throws SQLException {
		// initialization 
		try {
			this.output = new FileWriter(this.outputFile = output);
			init(false);
		} catch (IOException e) {
			// Could no write properties file
			System.out.println("IOException: " + e);
		}
	}

	public LP553554(File output553, File output554, boolean allowBlanks) throws SQLException {
		// initialization 
		try {
			this.output = new FileWriter(this.outputFile = output553);
			this.output2 = new FileWriter(this.outputFile2 = output554);
			init(allowBlanks);
		} catch (IOException e) {
			// Could no write properties file
			System.out.println("IOException: " + e);
		}
	}

	 private void init(boolean allowBlanks) throws IOException, SQLException {
			// Set the data and xref variables
		    data = DAOFactoryOracleDB.getDAOOracleDB(true);
			xref = new DAOFactoryAccessDB();
			xJobCodes = new XrefJobCode(allowBlanks);
			xPositions = new XrefPosition();
			xDepartments = new XrefDepartment(allowBlanks);
			xProcessLevels = new XrefProcessLevel();
			xEmpStatuses = new XrefEmpStatus();
			xLocatCodes = new XrefLocatCode(allowBlanks);
			xUnionCodes = new XrefUnionCode(allowBlanks);

			sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME;
			stmtData = data.getDataStatement();	    
			countStmtData = data.getCountStatement();
			//Set the Date Format for output
			df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
	}

public TblTAEmpServ findTAEmpServ() {
		try {
			whereClause = " WHERE (to_char(tr_date, 'yyyymmdd') > '20111231') and " +
		                  "(to_char(tr_date, 'yyyymmdd') < '20130101')"; 
			TblTAEmpServ tae = new TblTAEmpServ();
			sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME + (whereClause==null?"":whereClause);
			rslt = data.getDataStatement().executeQuery(sqlData);
			rsmd = rslt.getMetaData();
			if (rslt.next())
				hasMoreRecs = true;
			else
				hasMoreRecs = false;
			tae = (TblTAEmpServ) createObject(rslt, tae);
			return tae;
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		} catch (InvocationTargetException ite) {
			System.out.println("InvocationException: " + ite);
		} catch (IllegalAccessException iae) {
			System.out.println("IllegalAccessException: " + iae);
		}
	  return null;
}

public TblTAEmpServ findTADEmpSErv(String employeeNumber) {
	try {		
		sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME + " WHERE " + TABLE_NAME + "='" + employeeNumber + "'";
		rslt = data.getDataStatement().executeQuery(sqlData);
		rsmd = rslt.getMetaData();
		rslt.next();
		//@ TODO Add the creation of an Employee object here
		tae = (TblTAEmpServ) createObject(rslt, tae);
		return tae;
		//createFields(tae);
	} catch (SQLException e) {
	    // Could not connect to the database
		System.out.println("SQLException: " + e);
	} catch (InvocationTargetException ite) {
		System.out.println("InvocationException: " + ite);
	} catch (IllegalAccessException iae) {
		System.out.println("IllegalAccessException: " + iae);
	}
	return new TblTAEmpServ();
}

public TblTAEmpServ nextTAEmpServ() {
		try {
			if (rslt.next()) {
				hasMoreRecs = true;
				tae = (TblTAEmpServ) createObject(rslt, tae);
			}
			else
				hasMoreRecs = false;
			return tae;
			} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		} catch (InvocationTargetException ite) {
			System.out.println("InvocationException: " + ite);
		} catch (IllegalAccessException iae) {
			System.out.println("IllegalAccessException: " + iae);
		}
	  return null;
}

	public boolean hasMore () {
	  return hasMoreRecs;
	}

public int countTAEmpServ() {
	try {
		countRecs = data.getCountStatement().executeQuery("SELECT COUNT(*) FROM " + data.getSchema() + "." + TABLE_NAME + (whereClause==null?"":whereClause));
		countRecs.next();
		return recCount=countRecs.getInt(1);
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		}
		return 0;
}

public void setWhereClause(String where) {
	  this.whereClause = where;
}

public void run() {
//	  TblTAEmpServ tae = null;  
	  try {
		//Update current date/time
		Date now = new Date();
		
// How to write start to both files?		
		
		System.out.println(outputFile.getName());
		System.out.println(outputFile2.getName() + " Start " + df.format(now));
		//Write timestamp to output file
		output.write(outputFile.getName() + " Start " + df.format(now) + "\n");
		output2.write(outputFile2.getName() + " Start " + df.format(now) + "\n");
		
		tae = findTAEmpServ();
//		output.write(writeLP553Layout(tae) + "\n");
		while (hasMore()) {
			checkFileToWrite(); 
			tae = nextTAEmpServ();
		}
		//Update current date/time
		now = new Date();
		System.out.println(outputFile.getName());
		System.out.println(outputFile2.getName() + " Finish " + df.format(now));
		//Write timestamp to output file
		output.write(outputFile.getName() + " Finish " + df.format(now) + "\n");
		output2.write(outputFile2.getName() + " Finish " + df.format(now) + "\n");
		output.flush();
		output2.flush();
		output.close();
		output2.close();
	} catch (IOException e) {
		// Could not write properties file
		// Could not create output file
		System.out.println("IOException: " + e);
	} catch (ParseException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

public void checkFileToWrite () throws IOException, ParseException {

//	lp_status = tae.getR_status();
	if (tae.getR_status() == 9)
		{
		 output.write(writeLP553Layout(tae) + "\n");
		}
	else
		{
		 output2.write(writeLP554Layout(tae) + "\n");
		}
	
}	
	
	//Output the LP553 format
	public String writeLP553Layout(TblTAEmpServ tae) {
		return (
				"1" + "," +
				tae.getEmployee() + "," +
				tae.getTr_date() + "," +
				tae.getPer_end_date() + "," +
				tae.getService_code() + "," +
				tae.getReason_code() + "," +
				tae.getExt_time_seq() + "," +
				xProcessLevels.xlate(tae.getProcess_level()) + "," +	
				xDepartments.xlate(tae.getDepartment()) + "," +
				xPositions.xlate(tae.getPosition()) + "," +
				xJobCodes.xlate(tae.getJob_code()) + "," +
				tae.getHours() + "," +
				tae.getWage_amount() + "," +
				tae.getCurrency_code() + "," +
				tae.getPoints() + "," +
				tae.getOccurrence() + "," +
				tae.getCreate_date() + "," +
				tae.getCreate_time() + "," +
				tae.getCreate_user_id() + "," +
				tae.getDate_stamp() + "," +
				tae.getTime_stamp() + "," +
				tae.getUser_id()
				 

				);
	}
	
	//Output the LP553 format
	public String writeLP554Layout(TblTAEmpServ tae) {
		return (
				"1" + "," +
				tae.getEmployee() + "," +
				tae.getTr_date() + "," +
				tae.getPer_end_date() + "," +
				tae.getService_code() + "," +
				tae.getReason_code() + "," +
				tae.getExt_time_seq() + "," +
				xProcessLevels.xlate(tae.getProcess_level()) + "," +	
				xDepartments.xlate(tae.getDepartment()) + "," +
				xPositions.xlate(tae.getPosition()) + "," +
				xJobCodes.xlate(tae.getJob_code()) + "," +
				tae.getHours() + "," +
				tae.getWage_amount() + "," +
				tae.getCurrency_code() + "," +
				tae.getPoints() + "," +
				tae.getOccurrence() + "," +
				tae.getCreate_date() + "," +
				tae.getCreate_time() + "," +
				tae.getCreate_user_id() + "," +
				tae.getDate_stamp() + "," +
				tae.getTime_stamp() + "," +
				tae.getUser_id()
				 

				);
	}
}